import aioredis
from fastapi import FastAPI

app = FastAPI()


@app.on_event("startup")
async def startup_event():
    app.state.redis = await aioredis.from_url(
        "redis://localhost"
    )


@app.on_event("shutdown")
async def shutdown_event():
    app.state.redis.close()
    await app.state.redis.wait_closed()


@app.get("/redis/{key}")
async def read_item(key: str):
    value = await app.state.redis.get(key)
    return {"key": key, "value": value}
